package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.NodeComponentEditPolicy;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.execution.RuleExecution;
import edu.pku.sei.transformation.editor.views.figures.RuleExecutionFigure;
import edu.pku.sei.transformation.editor.views.figures.SimpleRuleFigure;
import edu.pku.sei.transformation.editor.views.figures.TagedElementFigure;

public class RuleExecutionEditpart extends AbstractNodeEditPart {

	public RuleExecutionEditpart(MetaShapeElement model) {
		super(model);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected IFigure createFigure() {
		RuleExecutionFigure figure = new RuleExecutionFigure();
		RuleExecution execution = (RuleExecution) getNode().getModelElement();
		figure.setName(execution.getName());
		if (execution.getSpecification() != null) 
			figure.setDescription(execution.getSpecification().getName());
		else
			figure.setDescription("undefine");
		return figure;
	}
	
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new NodeComponentEditPolicy());
	}
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		RuleExecutionFigure figure = (RuleExecutionFigure) getFigure();
		RuleExecution execution = (RuleExecution) getNode().getModelElement();
		String propertyName = event.getPropertyName();
		if (ModelPackage.NAMEDELEMENT_NAME.equals(propertyName)) {
			figure.setName(event.getNewValue().toString());
		} else if (ModelPackage.RULEEXE_SPCIFICATION.equals(propertyName)) {
			if (event.getNewValue() != null)
				figure.setDescription(event.getNewValue().toString());
			else
				figure.setDescription("undefine");
		} else {
			super.handlePropertyChanged(event);			
		}
	}
	
}
